package pl.symulatorbiegu;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.NumberPicker;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Map;

public class MainActivity extends ActionBarActivity {

    public void startClicked(View view) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Log.d("lnowak", "setcontentview");

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment implements View.OnClickListener {

        ArrayList<NumberPicker> pickers = new ArrayList<NumberPicker>();
        ArrayList<TextView> textViews = new ArrayList<TextView>();
        String[] pickerNumbers;
        HeatSimulator heatSimulator;

        public PlaceholderFragment() {
        }

        @SuppressLint("NewApi")
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            setupUI(rootView);

            return rootView;
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.button_start:
                    startClicked();
                    break;
            }
        }

        protected void startClicked() {
            ArrayList<Double> doubles = new ArrayList<Double>();
            for (NumberPicker np : pickers) {
                doubles.add(Double.valueOf(pickerNumbers[np.getValue()]));
            }
            heatSimulator = new HeatSimulator(doubles);
            heatSimulator.simulateHeat();
            Map<Integer, Integer> results = heatSimulator.getPointsMap();

            Map<Double, Integer> resultsMap = heatSimulator.resultsMap;

            for (Map.Entry<Double, Integer> entry : resultsMap.entrySet()) {
                Log.d("lnowak", entry.toString());
            }
            int i = 0;
            for (TextView tv : textViews) {
                Log.d("lnowak", "i = " + i + " value: " + results.get(i).toString());
                tv.setText(results.get(i++).toString());
            }
            Log.d("lnowak", " ---- ");
        }


        protected void setupUI(View rootView) {
            setupButtons(rootView);
            setupPickers(rootView);
            setupTextViews(rootView);
        }

        private void setupButtons(View rootView) {
            Button but = (Button) rootView.findViewById(R.id.button_start);
            but.setOnClickListener(this);
        }

        private void setupTextViews(View rootView) {
            textViews.add((TextView) rootView.findViewById(R.id.textView1));
            textViews.add((TextView) rootView.findViewById(R.id.textView2));
            textViews.add((TextView) rootView.findViewById(R.id.textView3));
            textViews.add((TextView) rootView.findViewById(R.id.textView4));
        }

        private void setupPickers(View rootView) {
            pickers.add((NumberPicker) rootView.findViewById(R.id.numberPicker1));
            pickers.add((NumberPicker) rootView.findViewById(R.id.numberPicker2));
            pickers.add((NumberPicker) rootView.findViewById(R.id.numberPicker3));
            pickers.add((NumberPicker) rootView.findViewById(R.id.numberPicker4));

            pickerNumbers = getPickerValues();

            for (NumberPicker np : pickers) {
                np.setMaxValue(pickerNumbers.length - 1);
                np.setMinValue(0);
                np.setValue(20);
                np.setWrapSelectorWheel(false);
                np.setDisplayedValues(pickerNumbers);
                np.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
            }
        }

        private String[] getPickerValues() {
            ArrayList<String> numbersDouble = new ArrayList<String>();
            for (Integer i = 0; i < 31; ++i)
                numbersDouble.add(String.valueOf((Double) (i / 10.0)));
            return numbersDouble.toArray(new String[numbersDouble.size()]);
        }
    }

}
